﻿using ConcurrencyAPI.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;

namespace ConcurrencyAPI.Repository
{
    public class ProductRepository:BaseRepository<Product>
    {
        private ConcurrencyContext conContext = new ConcurrencyContext();
        public List<Product> GetAllProducts()
        {
            return this.LoadEntities().ToList();
        }

        public Product GetProduct(int Id)
        {
            Expression<Func<Product, bool>> whereLambda= 
                t=>t.Id==Id;
            return this.LoadEntities(whereLambda).FirstOrDefault();
        }

        public List<Product> GetProduct(Expression<Func<Product, bool>> whereLambda)
        {
            return this.LoadEntities(whereLambda).ToList();
        }

        public Product AddProduct(Product product)
        {
            return AddEntity(product);
        }

        public bool UpdateProduct(Product product)
        {
            Product entity = this.LoadEntities(t => t.Id == product.Id).FirstOrDefault();
            entity.ProductName = product.ProductName;
            entity.Description = product.Description;
            return this.UpdateEntity(entity);
        }

        public bool DeleteProduct(int Id)
        {
            Product product = this.LoadEntities(t => t.Id == Id).FirstOrDefault();
            return this.DeleteEntity(product);
        }

    }
}